﻿
function onReady() {
    var accordion1 = Ext.getCmp(IDS.accordion1);
    var mainTabStrip = Ext.getCmp(IDS.mainTabStrip);
    var treeCM = Ext.getCmp(TreeMenus.treeCM);
    var treePM = Ext.getCmp(TreeMenus.treePM);
    var treeAdmin = Ext.getCmp(TreeMenus.treeAdmin);

    treeCM.on('click', function (node, event) {
        if (node.isLeaf()) {
            // 阻止事件传播
            event.stopEvent();
            //var href = node.attributes.href;
            // 修改地址栏
            // 新增Tab节点
            //addExampleTab(node);
        }
    });

    treePM.on('click', function (node, event) {
        if (node.isLeaf()) {
            // 阻止事件传播
            event.stopEvent();
            //var href = node.attributes.href;
            // 修改地址栏
            // 新增Tab节点
           // addExampleTab(node);
        }
    });

    treeAdmin.on('click', function (node, event) {
        if (node.isLeaf()) {
            // 阻止事件传播
            event.stopEvent();
            //var href = node.attributes.href;
            // 修改地址栏
            // 新增Tab节点
            //addExampleTab(node);
        }
    });
    





    // 动态添加一个标签页
    function addExampleTab(node) {
        var href = node.attributes.href;

        // 动态创建按钮
        var newButton = new Ext.Button({
            text: "Button控件",
            type: "button",
            cls: "x-btn-text-icon",
            icon: "./res.axd?icon=PageWhiteCode",
            listeners: {
                click: function (button, e) {
                    alert("暂未实现");
                    e.stopEvent();
                }
            }
        });

        var openNewWindowButton = new Ext.Button({
            text: '新标签页中打开',
            type: "button",
            cls: "x-btn-text-icon",
            icon: "./res.axd?icon=TabGo",
            listeners: {
                click: function (button, e) {
                    window.open(href, "_blank");
                    e.stopEvent();
                }
            }
        });

        var refreshButton = new Ext.Button({
            text: '刷新',
            type: "button",
            cls: "x-btn-text-icon",
            icon: "./res.axd?icon=Reload",
            listeners: {
                click: function (button, e) {
                    // 注意：button.ownerCt 是工具栏，button.ownerCt.ownerCt 就是当前激活的标签页。
                    Ext.DomQuery.selectNode('iframe', button.ownerCt.ownerCt.getEl().dom).contentWindow.location.reload(); //.replace(href);
                    e.stopEvent();
                }
            }
        });

        // 动态添加一个带工具栏的标签页
        var tabId = 'dynamic_added_tab' + node.id.replace('__', '-');
        var currentTab = mainTabStrip.getTab(tabId);
        if (!currentTab) {
            mainTabStrip.addTab({
                'id': tabId,
                'url': href,
                'title': node.text,
                'closable': true,
                'bodyStyle': 'padding:0px;',
                'iconCls': 'icon_' + href.replace(/[^.]+\./, ''),
                'tbar': new Ext.Toolbar({
                    items: ['->', newButton, '-', refreshButton, '-', openNewWindowButton]
                })
            });
        } else {
            mainTabStrip.setActiveTab(currentTab);
        }
    }

    mainTabStrip.on('tabchange', function (tabStrip, tab) {
        if (tab.url) {
            //window.location.href = '#' + tab.url;
            //window.location.hash = '#' + tab.url;
        } else {
           // window.location.hash = '#';
        }
    });

}
